﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_ValueRangeField:PageForm_InputFieldBase
    {
        private string _startName = "";
        private string _endName = "";
        private string _startValue = "";
        private string _endValue = "";
        private string _maxValue = "";
        private string _minValue = "";
        private string _startPlaceHolder = "";
        private string _endPlaceHolder = "";
        private bool _startReadonly = false;
        private bool _endReadonly = false;
        private InputElementType _type = InputElementType.text;
        private bool _startValueRequired = false;
        private bool _endValueRequired = false;

        public PageForm_ValueRangeField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_valuerangefield");
        }
        
        public PageForm_ValueRangeField Name(string startName,string endName)
        {
            _startName = startName;
            _endName = endName;
            return this;
        }

        public PageForm_ValueRangeField Value(string startValue,string endValue)
        {
            _startValue = startValue;
            _endValue = endValue;

            return this;
        }

        public PageForm_ValueRangeField ValueLimit(int maxValue, int minValue)
        {
            _maxValue = maxValue.ToStringEx();
            _minValue = minValue.ToString();

            return this;
        }

        public PageForm_ValueRangeField ValueLimit(decimal maxValue, decimal minValue)
        {
            _maxValue = maxValue.ToStringEx();
            _minValue = minValue.ToStringEx();

            return this;
        }

        public PageForm_ValueRangeField Type(InputElementType type)
        {
            _type = type;

            return this;
        }

        public PageForm_ValueRangeField Placeholder(string startValuePlaceholder, string endPlaceholder)
        {
            _startPlaceHolder = startValuePlaceholder;
            _endPlaceHolder = endPlaceholder;

            return this;
        }

        public PageForm_ValueRangeField ReadOnly(bool allReadonly = true)
        {
            _startReadonly = allReadonly;
            _endReadonly = allReadonly; ;

            return this;
        }

        public PageForm_ValueRangeField ReadOnly(bool startReadonly, bool endReadonly)
        {
            _startReadonly = startReadonly;
            _endReadonly = endReadonly;
            return this;
        }

        public PageForm_ValueRangeField Required(bool startValueRequired, bool endValueRequired)
        {
            _endValueRequired = endValueRequired;
            _startValueRequired = startValueRequired;

            return this;
        }

        public PageForm_ValueRangeField Required(bool allRequired=true)
        {
            _endValueRequired = allRequired;
            _startValueRequired = allRequired;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            _isRequired = _startValueRequired || _endValueRequired;

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine($"<input type=\"{_type}\" name=\"{_startName}\" {_startReadonly.If("readonly")} {(_startValueRequired ? "required" : "")} value=\"{_startValue}\" {(_maxValue.IfNotEmptyOrWhileSpace($"max=\"{_maxValue}\""))} {(_minValue.IfNotEmptyOrWhileSpace($"min=\"{_minValue}\""))}   class=\"form-control startvalue\" placeholder=\"{_startPlaceHolder}\">");
            writer.WriteLine("<span class=\"input-group-addon\">-</span>");
            writer.WriteLine($"<input type=\"{_type}\" name=\"{_endName}\" {_endReadonly.If("readonly")} {(_endValueRequired ? "required" : "")} value=\"{_endValue}\" {(_maxValue.IfNotEmptyOrWhileSpace($"max=\"{_maxValue}\""))} {(_minValue.IfNotEmptyOrWhileSpace($"min=\"{_minValue}\""))}   class=\"form-control endvalue\" placeholder=\"{_endPlaceHolder}\">");


            //writer.WriteLine($"<div class=\"col-xs-4\"><input type=\"{_type}\" name=\"{_startName}\" value=\"{_startValue}\" max=\"{_maxValue}\" min=\"{_minValue}\" class=\"form-control\" placeholder=\"{_startPlaceHolder}\"></div>");
            //writer.Write("-");
            //writer.WriteLine($"<div class=\"col-xs-4\"><input type=\"{_type}\" name=\"{_endName}\" value=\"{_endValue}\"  max=\"{_maxValue}\" min=\"{_minValue}\"  class=\"form-control\" placeholder=\"{_endPlaceHolder}\"></div>");
        }

        protected override string ErrorMessage
        {
            get
            {
                return GetErrorMessageByNames(_startName, _endName);

                //var errorMsg = "";

                //if (ViewContext.ModelState.TryGetValue(_startName, out var error)) // ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);
                //{
                //    errorMsg= error.Errors.Select(x => x.ErrorMessage).JoinToString('|');
                //}

                //if (ViewContext.ModelState.TryGetValue(_endName, out var error1)) // ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);
                //{
                //    errorMsg = errorMsg +  error1.Errors.Select(x => x.ErrorMessage).JoinToString('|');
                //}

                //return errorMsg;
            }
        }

        protected override bool HasError
        {
            get
            {
                return IsHasErrorByNames(_startName, _endName);

                //var hasError = false;

                //if (ViewContext.ViewData.ModelState.TryGetValue(_startName, out var errors))
                //{
                //    hasError =  errors.Errors.HasData();
                //}

                //if (hasError && ViewContext.ViewData.ModelState.TryGetValue(_endName, out var errors1))
                //{
                //    hasError = errors1.Errors.HasData();
                //}

                //return hasError;
            }
        }
    }
}
